Simple Pendulum

In this notebook, we're solving the simple pendulum problem analytically and numerically. The simple pendulum is a classical example of a nonlinear, second-order, ordinary differential equation (ODE).

Problem Statement

The simple pendulum is a mass m attached to a massless, frictionless rod of length l that is free to rotate about a fixed point. The pendulum is released from rest at an angle θ0 from the vertical

Necessary libraries

  • numpy - for numerical calculations
  • matplotlib - for plotting
  • scipy.integrate - for solving ODEs
  • sympy - for symbolic calculations

Run the next cell in case you don't have these libraries installed.

In [2]:
Note: you may need to restart the kernel to use updated packages.
In [1]:

Definitions

We need to define the following constants and variables:

  • g - acceleration due to gravity
  • l - length of the pendulum
  • m - mass of the pendulum
  • θ - angle of the pendulum from the vertical (function of time)
  • t - time
In [2]:

Now we need to define x and y in terms of θ.

  • x - horizontal position of the pendulum - x=lsin(θ)
  • y - vertical position of the pendulum - y=lcos(θ)
In [3]:

Once we have x and y, we can get the kinetic and potential energies of the pendulum.

  • T - kinetic energy of the pendulum
    T=12mx˙2+12my˙2
  • V - potential energy of the pendulum
    V=mgy
In [4]:

Now, we can get the Lagrangian of the pendulum.

  • L - Lagrangian of the pendulum - L=TV
In [5]:
In [6]:
Out[6]:
lm(2gcos(θ(t))+l(ddtθ(t))2)2

With the Lagrangian, we can get the equations of motion.

ddtLθ˙Lθ=0

In [7]:

Now, let's solve the equation for θ¨.

In [8]:
In [9]:
Out[9]:
gsin(θ(t))l

With the analitycal solution for θ¨, we can get the numerical solution for θ˙ and θ.

In [10]:

Let's define a vector S that returns dSdt.

In [11]:

Now, let's plot the results.

In [12]:
In [16]:

Plot phase space using streamplots.

In [17]:

To create an animation, we need to define a function that returns x and y coordinates for each frame.

In [18]:
In [19]:
Out[19]:
In [ ]: